/**
 * @file tablewidget.cpp
 * @brief TableWidget class methods implementation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * @author VestniK (Sergey N.Vidyuk) sir.vestnik@gmail.com
 * @date 13 Jul 2010
 */
#include "tablewidget.h"

#include <QtCore/QSettings>

using namespace mjn;

mjn::TableWidget::TableWidget(QWidget* parent): QGLWidget(parent)
{
    QSettings settings;
    settings.beginGroup("scene");

    mProectionAngle = settings.value("proectinAngle",60.0).toDouble();
    mNearPlaneDist = settings.value("nearPlaneDist",5.0).toDouble();
    mFarPlaneDist = settings.value("farPlaneDist",60.0).toDouble();
    // Camera position
    mCameraPos[0] = settings.value("cameraX",-20.0).toDouble();
    mCameraPos[1] = settings.value("cameraY",0.0).toDouble();
    mCameraPos[2] = settings.value("cameraZ",2.0).toDouble();
    // Scene center position
    mCenterPos[0] = settings.value("centerX",0.0).toDouble();
    mCenterPos[1] = settings.value("centerY",0.0).toDouble();
    mCenterPos[2] = settings.value("centerZ",0.0).toDouble();
    // Up direction
    mUpDirection[0] = settings.value("upX",0.0).toDouble();
    mUpDirection[1] = settings.value("upY",0.0).toDouble();
    mUpDirection[2] = settings.value("upZ",1.0).toDouble();

    // Global ambient light
    mAmbientLight[0] = settings.value("globalAmbientLightR",0.2).toDouble();
    mAmbientLight[1] = settings.value("globalAmbientLightG",0.2).toDouble();
    mAmbientLight[2] = settings.value("globalAmbientLightB",0.2).toDouble();
    mAmbientLight[3] = settings.value("globalAmbientLightA",1.0).toDouble();
    // Light 0 position
    mLight0Pos[0] = settings.value("light0PositionX",0.0).toDouble();
    mLight0Pos[1] = settings.value("light0PositionY",0.0).toDouble();
    mLight0Pos[2] = settings.value("light0PositionZ",1.0).toDouble();
    mLight0Pos[3] = settings.value("light0PositionW",0.0).toDouble();
    // Light 0 direction
    mLight0Dir[0] = settings.value("light0DirectionX",0.0).toDouble();
    mLight0Dir[0] = settings.value("light0DirectionY",0.0).toDouble();
    mLight0Dir[0] = settings.value("light0DirectionZ",-1.0).toDouble();
    // Light 0 angle
    mLight0SpotAngle = settings.value("light0SpotAngle",180.0).toDouble();
    settings.endGroup();

    mLoader.load(":/models/tile.obj");
}

void mjn::TableWidget::initializeGL()
{
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    qglClearColor(Qt::black);
    glClearDepth(mFarPlaneDist);

    mLoader.buildList();
}

void mjn::TableWidget::resizeGL(int width, int height)
{
    glViewport(0, 0, (GLsizei)width, (GLsizei)height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(mProectionAngle, (GLfloat)width/(GLfloat)height,
                   mNearPlaneDist, mFarPlaneDist);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(mCameraPos[0]  , mCameraPos[1]  , mCameraPos[2],
              mCenterPos[0]  , mCenterPos[1]  , mCenterPos[2],
              mUpDirection[0], mUpDirection[1], mUpDirection[2]);

    glLightModelfv(GL_LIGHT_MODEL_AMBIENT,mAmbientLight);
    glLightfv(GL_LIGHT0, GL_POSITION, mLight0Pos);
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, mLight0Dir);
    glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, mLight0SpotAngle);
}

void mjn::TableWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glCallList(mLoader.list());

    glFlush();
}
